﻿using System;
using System.Linq;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using zijian666.DI;

namespace UnitTests;

[TestClass]
public class 命名服务测试
{

    [TestMethod]
    public void Test1()
    {
        var p = new ServiceCollection()
                    .AddNamedSingleton("你的名字", "zijian666")
                    .BuildServiceProvider();

        Assert.AreEqual(p.GetNamedService<string>("你的名字"), "zijian666");
        Assert.AreEqual(p.GetNamedService<object>("你的名字"), "zijian666");
        Assert.AreEqual(p.GetNamedServices<string>("你的名字").FirstOrDefault(), "zijian666");
        Assert.IsNull(p.GetNamedService<string>("我的名字"));


    }


    [TestMethod]
    public void Test2()
    {
        var p = new ServiceCollection()
                    .AddNamedDelegate("委托", (Func<object, string>)(x => x + "_123"))
                    .BuildServiceProvider();

        var func1 = p.GetNamedDelegate<Func<object, string>>("委托");
        Assert.IsNotNull(func1);
        Assert.AreEqual(func1("111"), "111_123");

        var func2 = p.GetNamedDelegate<Func<string, object>>("委托");

        Assert.IsNotNull(func2);
        Assert.AreEqual(func2("111"), "111_123");

    }
}
